package com.bunnyhsu.configuration;

import org.springframework.boot.context.embedded.EmbeddedServletContainerInitializedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * Created by xl on 2018/5/17.
 */
@Component
public class HostConfig implements ApplicationListener<EmbeddedServletContainerInitializedEvent> {

    private int serverPort;
    private String host = null;
    @Override
    public void onApplicationEvent(EmbeddedServletContainerInitializedEvent event) {
        this.serverPort = event.getEmbeddedServletContainer().getPort();
    }
    public String getPort() {
        try {
            host = InetAddress.getLocalHost().getHostAddress();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        return host+":"+this.serverPort;
    }


}
